using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;

namespace EntGuayasSLA.BL
{
    [Serializable]
    public class Documentos : BusinessBase<Documentos>
    {
        #region Business Methods

        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(c =>c.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        public static readonly PropertyInfo<string> PersonaIdProperty = RegisterProperty<string>(c => c.PersonaId);
        public string PersonaId
        {
            get { return GetProperty(PersonaIdProperty); }
            set { SetProperty(PersonaIdProperty, value); }
        }

        public static readonly PropertyInfo<string> ArchivoIdProperty = RegisterProperty<string>(c => c.ArchivoId);
        public string ArchivoId
        {
            get { return GetProperty(ArchivoIdProperty); }
            set { SetProperty(ArchivoIdProperty, value); }
        }
        public static readonly PropertyInfo<string> TipoProperty = RegisterProperty<string>(c => c.Tipo);
        public string Tipo
        {
            get { return GetProperty(TipoProperty); }
            set { SetProperty(TipoProperty, value); }
        }
        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            base.AddBusinessRules();

            //BusinessRules.AddRule(new Rule(IdProperty));
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion

        #region Factory Methods

        public static Documentos NewEditableRoot()
        {
            return DataPortal.Create<Documentos>();
        }

        public static Documentos GetEditableRoot(int id)
        {
            return DataPortal.Fetch<Documentos>(id);
        }

        public static void DeleteEditableRoot(int id)
        {
            DataPortal.Delete<Documentos>(id);
        }

        private Documentos()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(int criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_dpe_id", ReadProperty(IdProperty)) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_DOCUMENTO\"", parametros);
            if (datos.Tables[0].Rows.Count > 0)
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0][0].ToString()));
                LoadProperty(PersonaIdProperty, datos.Tables[0].Rows[0][1].ToString());
                LoadProperty(ArchivoIdProperty, datos.Tables[0].Rows[0][2].ToString());
                LoadProperty(TipoProperty, datos.Tables[0].Rows[0][3].ToString());
             
            }
        }
       
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_dpe_id", ReadProperty(IdProperty)),
                new Parametro("ec_per_id", ReadProperty(PersonaIdProperty)),
                new Parametro("ec_arc_id", ReadProperty(ArchivoIdProperty)),
                new Parametro("ec_dpe_tipo", ReadProperty(TipoProperty))
            };
            accesoDatos.EjecutarQuery("\"INS_DOCUMENTO\"", parametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_dpe_id", ReadProperty(IdProperty)),
                new Parametro("ec_per_id", ReadProperty(PersonaIdProperty)),
                new Parametro("ec_arc_id", ReadProperty(ArchivoIdProperty)),
                new Parametro("ec_dpe_tipo", ReadProperty(TipoProperty))
            };
            accesoDatos.EjecutarQuery("\"ACT_DOCUMENTO\"", parametros);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(this.Id);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_dpe_id", ReadProperty(IdProperty))
            };
            accesoDatos.EjecutarQuery("\"ELI_DOCUMENTO\"", parametros);
        }

        #endregion
    }
}
